$().ready(function() {
	loadProject();
	loadComplany();
	validateRule();
	laydate({
		elem : '#receiptDate'
	});
	laydate({
		elem : '#arrivalDate'
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/fin/receipt/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			receiptDate : {
				required : true
			},
			projectId: {
				required : true
			},
			recriptCompanyid: {
				required : true
			},
			receiptAmount: {
				required : true
			},
			receiptScale: {
				required : true
			}
		},
		messages : {
			receiptDate : {
				required : icon + "请选择收款时间"
			},
			projectId: {
				required : icon + "请选择所属项目"
			},
			recriptCompanyid: {
				required : icon + "请选择收款方"
			},
			receiptAmount: {
				required : icon + "请输入收款金额"
			},
			receiptAmount: {
				required : icon + "请输入本次收款比例"
			}
		}
	})
}
//项目
function loadProject(){
	var html = "";
	$.ajax({
		url : '/itm/project/getNotEndProjectlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-projectId").append(html);
			$(".chosen-select-projectId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-projectId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				$.ajax({
					url :"/itm/project/get/"+params.selected,
					data:null,
					type:"get",
					success : function(data) {
						$("input[name='projectAmount']").val(Number(data.projectAmount));
					}
				});
			});
		}
	});
}
//收款方
function loadComplany(){
	var html = "";
	$.ajax({
		url : '/system/company/getCompanyList',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-recriptCompanyid").append(html);
			$(".chosen-select-recriptCompanyid").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-recriptCompanyid').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

layui.use('upload', function(){
	var upload = layui.upload;
	//执行实例
	var uploadInst = upload.render({
		elem: '#upLoad' //绑定元素
		,url: '/common/sysFile/upload' //上传接口
		,before: function(obj){
			//预读本地文件示例，不支持ie8
			obj.preview(function(index, file, result){
				$('#pic').attr('src', result); //图片链接（base64）
			});
		}
		,done: function(res){
			//上传完毕回调
			console.log(res,"fileName");
			$('#receiptProof').val(res.fileName)
		}
		,error: function(){
			//请求异常回调
		}
	});
})

function deviceAmount() {
	var projectAmount = Number($("input[ name='projectAmount']").val());
	var receiptAmount = Number($("input[ name='receiptAmount']").val());

	var aaa = receiptAmount/projectAmount*100
	aaa = aaa.toFixed(2);
	$("#receiptScale").val(aaa)
	$("#receiptScalestr").val((aaa)+"%")
}



